//
//  YFForgotPasswordVC.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/13.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFForgotPasswordVC.h"
#import "YFCustomTextField.h"
#import "YFLoginHelper.h"
#import "YFLoginApi.h"
#import "NSAttributedString+RichText.h"

NSString *const kFindPasswordCompleted = @"FindPasswordCompleted";

@interface YFForgotPasswordVC ()
@property (weak, nonatomic) IBOutlet UIButton *sendMailButton;
@property (weak, nonatomic) IBOutlet YFCustomTextField *emailTextfield;

@property (weak, nonatomic) IBOutlet UIView *finishSuperView;

@property (weak, nonatomic) IBOutlet UILabel *messageLabelOne;

@property (weak, nonatomic) IBOutlet UILabel *messageLabelTwo;

@property (weak, nonatomic) IBOutlet UIButton *tryAnginButton;
@property (weak, nonatomic) IBOutlet UIButton *finishButton;
@end

@implementation YFForgotPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Reset Password";
    [self setContinueViewUI];
    self.finishSuperView.hidden = YES;
    
}
- (IBAction)finishBtnClick:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:kFindPasswordCompleted object:self.emailTextfield.text];
}

- (void)setContinueViewUI {
    self.emailTextfield.text = self.emailString;
    self.emailTextfield.backgroundColor = [UIColor clearColor];
    [self.emailTextfield changeTextfieldStyle:LSTextfieldViewTypeEmail];
    self.emailTextfield.placeHolderAlignment = NSTextAlignmentCenter;
    self.emailTextfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Email" attributes:@{NSForegroundColorAttributeName:kTLightGray999}];
    [self.emailTextfield changeCornerRadius:0 borderWidth:1 borderColor:[UIColor whiteColor]];
    [self.sendMailButton clipsCornerRadius:3];
    [self.finishButton clipsCornerRadius:3];
    
    self.emailTextfield.showRightButton = YES;
    self.emailTextfield.rightButton.hidden = YES;
    [self.emailTextfield.rightButton setImage:[UIImage imageNamed:@"login_field_delete"] forState:UIControlStateNormal];
    __weak typeof(self) weakSelf = self;
    [self.emailTextfield setClickRightButtonHandler:^(NSString *text) {
        weakSelf.emailTextfield.text = @"";
    }];
    [self.emailTextfield setTextFieldDidBenginEidtHandler:^(NSString *text) {
        weakSelf.emailTextfield.rightButton.hidden = NO;
    }];
    [self.emailTextfield setTextFieldDidEndEidtHandler:^(NSString *text) {
        weakSelf.emailTextfield.rightButton.hidden = YES;
    }];
    
}

// 设置发送邮件成功后的UI
- (void)setMessageLabelTextWithUserEmail:(NSString *)email {
    NSString *messageStrOne = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"An e-mail has been sent to:\n", nil),email];
    self.messageLabelOne.attributedText = [NSAttributedString attributedString:messageStrOne subString:@[NSLocalizedString(@"An e-mail has been sent to:\n", nil),email] colors:@[kTLightGray999,kTWhiteText] fonts:@[[UIFont systemFontOfSize:20],[UIFont systemFontOfSize:20]]];
    
    self.messageLabelTwo.text = NSLocalizedString(@"Please check your inbox for a\nmessage form Coollang and follow\nthe insrtuctions in the message.", nil) ;
    
    NSString *messageStrThree = [NSString stringWithFormat:NSLocalizedString(@"If you do not receive the e-mail,\nplease check your spam folder or\n%@ later.", nil),NSLocalizedString(@"try again", nil)];
    
    NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithString:messageStrThree];
    
    [attrStrM addAttributes:@{NSForegroundColorAttributeName:kTWhiteText,NSUnderlineColorAttributeName:kTWhiteText,NSUnderlineStyleAttributeName:@(1)} range:[messageStrThree rangeOfString:NSLocalizedString(@"try again", nil)]];
    self.tryAnginButton.titleLabel.numberOfLines = 0;
    [self.tryAnginButton setTitleColor:kTLightGray999 forState:UIControlStateNormal];
    [self.tryAnginButton setAttributedTitle:attrStrM.copy forState:UIControlStateNormal];
}

- (IBAction)tryAnainButtonClick:(id)sender {
    [self sendMailBtnClick:nil];
}

- (IBAction)sendMailBtnClick:(id)sender {
    [self.emailTextfield resignFirstResponder];
    if (![YFLoginHelper checkEmail:self.emailTextfield.text]) {
        FUIAlertView *alertView = [FUIAlertView alertWithTitle:@"Warm Prompt" message:@"Please enter a valid email address" cancelTitle:@"OK" confirmTitle:nil];
        [alertView show];
        return;
    }
    YFLoginApi *forgotPasswordApi = [YFLoginApi forgotpasswordWithEmail:self.emailTextfield.text];
    [self.hud show];
    __weak typeof(self) weakSelf = self;
    [[YFNetworkingService sharedInstance] sendRequest:forgotPasswordApi callback:^(BOOL success, id result, NSError *error) {
        [self.hud dismiss];
        if (success) {
            if (weakSelf.finishSuperView.hidden ==  YES) {
                weakSelf.finishSuperView.hidden = NO;
                [weakSelf setMessageLabelTextWithUserEmail:self.emailTextfield.text];
            }else {
                FUIAlertView *alertView = [FUIAlertView alertWithTitle:@"Warm Prompt" message:@"An new email has been sent successfully！" cancelTitle:@"OK" confirmTitle:nil];
                [alertView show];
            }
        }else {
            // 用户不存在
            // 操作太频繁，一分钟只能发送一封邮件
            FUIAlertView *alertView = [FUIAlertView alertWithTitle:@"Warm Prompt" message:error.domain cancelTitle:@"OK" confirmTitle:nil];
            [alertView show];
        }
    }];
}

@end
